"""

python pymysql库的基础操作
"""
from pymysql import Connection

# 构建到MySQL数据库的链接
conn = Connection(
    host="localhost",  # 主机名（IP）
    port=3306,  # 端口
    user='root',  # 账户
    password='123456'  # 密码
)

# print(conn.get_server_info())

# 执行非查询性质SQL

# 获取到游标对象
cursor = conn.cursor()
# 选择数据库
# conn.select_db("test")
conn.select_db("world")
# 执行sql
# cursor.execute("create table test_pymysql2(id int)")

# 执行查询性质SQL
cursor.execute("select * from student")
results: tuple = cursor.fetchall()

# print(results)
# for r in results:
#     print(r)

# 将获取到的sql结果转化为字典
title = [title[0] for title in cursor.description]
# 定义一个空列表
res = []
for item in results:
    res.append(dict(zip(title, item)))
# print(res)
for i in res:
    print(i)
# 关闭链接
conn.close()
